package action.borrowinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import bizlogic.bookinfo.BookInfoService;
import bizlogic.bookinfo.domain.BookInfo;
import bizlogic.borrowinfo.BorrowInfoService;
import bizlogic.borrowinfo.domain.BorrowInfo;

@WebServlet("/borrowinfoaddaction")
public class BorrowinfoAddAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
     BorrowInfoService borrowInfoService=new BorrowInfoService();  

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if("getNextID".equals(request.getParameter("method"))) {
			getNextID(request, response);
		}
		if("addBorrowInfo".equals(request.getParameter("method"))) {
			addBorrowInfo(request, response);
		}
		if("queryBookinfo".equals(request.getParameter("method"))) {
			queryBookinfo(request, response);
		}
		if("queryBookinfoRemain".equals(request.getParameter("method"))) {
			queryBookinfoRemain(request, response);
		}
		if("getBookNameById".equals(request.getParameter("method"))) {
			 getBookNameById(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/*
     * queryBookinfo查询所有图书名字
     */
	public void  queryBookinfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BookInfoService bookInfoService=new BookInfoService();
		List<BookInfo> allbookinfo = bookInfoService.queryBookinfoByKeyWords(0, bookInfoService.findLength(),null, null);
		List<Map<String, Object>> bookText=new ArrayList<>();
		for(BookInfo bInfo:allbookinfo) {
			Map<String, Object> map=new HashMap<String, Object>();
			String intext=bInfo.getBookname()+"(ʣ�ࣺ"+bInfo.getRemain()+")";
			map.put("id", bInfo.getBookid());
			map.put("text", intext);
			bookText.add(map);
		}
		response.getWriter().write(JSONObject.toJSONString(bookText));
	}
	
	/*
     * getborrowid获取自增id
     */
	public void getNextID(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String string=borrowInfoService.getNextID();
		response.getWriter().write(string);
	}
	
	/*
     * 功能需求：实例化一个Bookinfo实体，调用service层的相关方法，新增借书信息（当剩余为0本时，不让借书）
     */
	public void addBorrowInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String json=request.getParameter("data");
		BorrowInfo borrowInfo=JSON.parseObject(json,BorrowInfo.class );
		BookInfoService bookInfoService=new BookInfoService();
		BookInfo queryBookInfoByID = bookInfoService.queryBookInfoByID(borrowInfo.getBookid());
		int remain=queryBookInfoByID.getRemain();
		if(remain>0) {
			String string=borrowInfoService.addBorrowInfo(borrowInfo);
			response.getWriter().write(string);
		}else {
			response.getWriter().write("ͼ�鱻������");
		}
		
	}
	
	/*
     * 根据id查询剩余数量
     */
	public void queryBookinfoRemain(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookid=request.getParameter("bookid");
		BookInfoService bookInfoService=new BookInfoService();
		BookInfo queryBookInfoByID = bookInfoService.queryBookInfoByID(bookid);
		String remain=queryBookInfoByID.getRemain().toString();
		response.getWriter().write(remain);
	}
	
	/*
     * getBookNameById根据bookid，将id转化为bookname
     */
	public void getBookNameById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String bookid=request.getParameter("bookid");
		BookInfoService bookInfoService=new BookInfoService();
		BookInfo queryBookInfoByID = bookInfoService.queryBookInfoByID(bookid);
		String bookname=queryBookInfoByID.getBookname();
		response.getWriter().write(bookname);
	}
}
	
